import { ref } from 'vue'
import { getNetworkOptionsRequestParam, NetowrkOptional } from './types'
import { getNetworkSelect } from '@/api/network'

const useNetworkOptions = () => {
  const networkOptions = ref<Array<NetowrkOptional>>([])
  const getNetworkOptionsLoading = ref(false)

  const getRequestParam = (requestiParam: getNetworkOptionsRequestParam) => {
    const result = {
      area: '',
      city: '',
      province: '',
      limit: 0,
      name: requestiParam.keyword,
      page: 0,
      type: requestiParam.queryCondition,
      typeId: '',
      areaType:requestiParam.areaType
    }

    if (requestiParam.queryCondition === 2 || requestiParam.queryCondition === 3) {
      result.province = requestiParam.province
      result.city = requestiParam.city
      result.area = requestiParam.area
    }

    if (requestiParam.queryCondition === 1 && requestiParam.networkType !== '0') {
      result.typeId = requestiParam.networkType
    }

    return result
  }

  const getNetworkOptions = async (requestiParam: getNetworkOptionsRequestParam) => {
    getNetworkOptionsLoading.value = true
    const res = await getNetworkSelect(getRequestParam(requestiParam))
    networkOptions.value = res.data.success ? res.data.data.rows : []
    getNetworkOptionsLoading.value = false
  }

  const clearNetworkOptions = () => {
    networkOptions.value = []
  }

  return {
    networkOptions,
    getNetworkOptionsLoading,
    getNetworkOptions,
    clearNetworkOptions,
  }
}

export default useNetworkOptions
